﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using NIEN_LUAN.Db;
using System.Windows.Forms;

namespace NIEN_LUAN
{
    public partial class FormThongKeHangTon : Form
    {
        private DataContentDataContext db = new DataContentDataContext();

        public FormThongKeHangTon()
        {
            InitializeComponent();
        }

        private void btnChonMH_Click(object sender, EventArgs e)
        {
            Form frmChonMH = new FormTimChiTietMatHang();
            frmChonMH.ShowDialog();
        }

        private void btnnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThongKe_Click(object sender, EventArgs e)
        {
            if (this.radioButtonLH.Checked==false && this.radioButtonSL.Checked==true )
            {                
                if (this.numericUDSL.Value <= 0)
                {
                    MessageBox.Show("Số lượng hàng chưa chọn", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                var mathang = (from m in db.MATHANGs
                               where (m.SoLuongHienCo.Value <= this.numericUDSL.Value)
                              // select m
                               select new Db.Chitietchungtuthongke
                               {
                                   //ben grid co thuoc tinh gi thi em bo vo dayy
                                   MaHang = m.MaHang,
                                   TenHang = m.TenHang,
                                   DonGiaXuat = m.GiaBan ?? 0,
                                   GiaNhap = m.GiaNhap ?? 0,
                                   SoLuongTon = m.SoLuongHienCo ?? 0,
                                   MaLoaiHang = m.LOAIHANG.TenLoaiHang,
                                   DonViTinh = m.DONVITINH.TenDonViTinh
                               }
                               ).ToList();
                this.mATHANGBindingSource.DataSource = mathang;
                this.mATHANGBindingSource.ResetBindings(true);
                return;
            }

            if (this.radioButtonLH.Checked==true && this.radioButtonSL.Checked==false)
            {

                if (string.IsNullOrEmpty(this.comboBoxloaihang.Text))
                {
                    MessageBox.Show("Bạn chưa chọn loại hàng", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
               
                var mathang = (from m in db.MATHANGs
                               where (m.MaLoaiHang == this.comboBoxloaihang.Text)
                               select new Db.Chitietchungtuthongke
                               {
                                   //ben grid co thuoc tinh gi thi em bo vo dayy
                                   MaHang = m.MaHang,
                                   TenHang = m.TenHang,
                                   DonGiaXuat = m.GiaBan ?? 0,
                                   GiaNhap = m.GiaNhap ?? 0,
                                   SoLuongTon = m.SoLuongHienCo ?? 0,
                                   MaLoaiHang = m.LOAIHANG.TenLoaiHang,
                                   DonViTinh = m.DONVITINH.TenDonViTinh
                               }).ToList();
                this.mATHANGBindingSource.DataSource = mathang;
                this.mATHANGBindingSource.ResetBindings(true);
                return;
            }
        }
     
        private void btnChon_Click(object sender, EventArgs e)
        {
            Form frmChonLoaiHang = new FormTimKiemLoaiHang(this);
            frmChonLoaiHang.ShowDialog();
        }

        private void FormThongKeHangTon_Load(object sender, EventArgs e)
        {
            this.dataGridViewHangTon.AutoGenerateColumns = false;
            var mathang = (from m in db.MATHANGs
                           orderby m.MaHang ascending
                           select new Db.Chitietchungtuthongke
                           {
                               //ben grid co thuoc tinh gi thi em bo vo dayy
                               MaHang = m.MaHang,
                               TenHang = m.TenHang,
                               DonGiaXuat = m.GiaBan ?? 0,
                               GiaNhap = m.GiaNhap ?? 0,
                               SoLuongTon = m.SoLuongHienCo ?? 0,
                               MaLoaiHang = m.LOAIHANG.TenLoaiHang,
                               DonViTinh = m.DONVITINH.TenDonViTinh
                           }
                               ).ToList();
            this.mATHANGBindingSource.DataSource = mathang;

            var lh = (from m in db.LOAIHANGs
                      select m).ToList();
            bsloaihang.DataSource = lh;
            this.comboBoxloaihang.DataSource = bsloaihang;
            this.comboBoxloaihang.DisplayMember = "MaLoaiHang";
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            Form frmthongkehangton = new FormReportHangTon(this);
            frmthongkehangton.ShowDialog();
        }     
    }

    public class NgayThang
    {
        public int Thang { get; set; }
        public int Nam { get; set; }
        public int Ngay { get; set; }
        public string ThangName { get; set; }
        public string NamName { get; set; }
    }
}
